import GameObject from "./GameObject"
import Utils from "./utils";
import LevelManager from "./levelmanager";
import SoundsManager from "./soundmanager";
import ClearData from "./ClearData";
import GameConst from "./GameConst";
import BonusBombSprite from "./amn/BonusBombSprite";
import BonusLineSprite from "./amn/BonusLineSprite";
import AnimationObject from "./animationobj";
import SinglePlayObject from "./amn/SinglePlayObject";
import CookieTweenSprite from "./CookieTweenSprite";
export default class CellObject extends GameObject {
    constructor(t, n) {
        super();
        this._isBlock = false;
        this.isTempBlock = false;
        this.tempBlockTime = -1;
        this.breakCountLeft = 0;
        this.marksLeft = 0;
        this.mark = null;
        this.block = null;
        this.blockTop = null;
        this.colorBlock = -1;
        this.bonusComboCount = -1;
        this.scoreToAdd = -1;
        this.breakMatchId = -1;
        this.changeTypeIn = -1;
        this.typeToChange = -1;
        this.clearsIn = [];
        this.removeBlockIn = -1;
        this.isBombBlocked = false;
        this.timeSinceBlockRemove = 100;
        this.teleportCellStart = null;
        this.teleportCellEnd = null;
        this.isTeleportLocked = false;
        this.lastBlockSet = -1;
        this.removeBlockLock = -1;
        this.fullClearsIn = [];
        this.isWaterBlock = false;
        this.isPlantBlock = false;
        this.plantBreaksLeft = -1;
        this.dontRemoveMark = false;
        this.prevMarkAlpha = 1;
        this.isPushEnabled = true;
        this.teleports = [];
        this.lastTeleportMove = -1;
        this.timeSinceLastTypeChange = 0;
        this.x = t;
        this.y = n;
        this.pos = Utils.cjp(Utils.gridToStageX(t), Utils.gridToStageY(n));
        // var r = GameConst.level.tileSize;
        // this.rect = new createjs.Rectangle(this.pos.x - r / 2, this.pos.y - r / 2, r, r);
    }

    destroy () {
        this.clearCell(false);
       Utils.removeClip(this.mark);
        this.mark = null;
       Utils.removeClip(this.teleportSprite);
        this.teleportSprite = null;
        if (this.blockAnimation)
            this.blockAnimation.destroy();
        this.blockAnimation = null;
       Utils.removeClip(this.blockTop);
        this.blockTop = null;
       Utils.removeClip(this.block);
        this.block = null;
       super.destroy(); 
    };

    update(dt) {
        if (this.isTempBlock) {
            this.tempBlockTime -= dt;
            if (this.tempBlockTime < 0) {
                this.setTempBlock(false);
                this.isTempBlock = false
            }
        }
        if (this.mark && this.mark.alpha != this.prevMarkAlpha) {
            this.prevMarkAlpha = this.mark.alpha;
            GameConst.level.needToUpdateBack = true
        }
        // if (this.teleportSprite && this.teleportSprite.currentAnimationFrame >= 11)
        //     this.teleportSprite.gotoAndStop(this.teleportCellEnd ? "teleport_in" : "teleport_out");
        var t = this.clearsIn.length;
        for (var n = 0; n < t; ++n) {
            var r = this.clearsIn[n];
            if (r.delay > 0) {
                r.delay -= dt;
                if (r.delay <= 0) {
                    this.clearsIn.splice(n, 1);
                    n--;
                    t--;
                    this.clearCell(r.clickOrColor)
                }
            } else {
                this.clearsIn.splice(n, 1);
                n--;
                t--;
            }
        }
        if (this.isPlantBlock) {
            if (this.block && this.block.visible && this.plantBreaksLeft <= 1 && this.block.currentAnimationFrame >= 19) {
                this.block.visible = false;
                this.block.stop();
            } else if (this.plantBreaksLeft == 1 && this.breakMatchId >= 0 && this.breakMatchId <= GameConst.level.currentMove - 3 && !this.block.visible && !GameConst.level.isHardLocked && this.object) {
                this.block.visible = true;
                this.block.gotoAndPlay("bush_anim");
                this.block.pivotX = this.block.pivotY = 35;
                this.plantBreaksLeft = 2;
                this._isBlock = true;
                this.block.framerate = 30;
                this.setTempBlock(true, Math.max(this.tempBlockTime, 20 / this.block.framerate));
            } else if (this.plantBreaksLeft == 2 && this.block.visible && this.block.currentAnimationFrame >= 19) {
                this.block.stop();
            }
        }
        if (this.blockAnimation) {
            this.blockAnimation.update(dt);
            if (this.blockAnimation.isDestroyed || !this.blockAnimation.sprite)
                this.blockAnimation = null;
        }
        if (!this._isBlock || this.breakCountLeft <= 0)
            this.timeSinceBlockRemove += dt;
        var t = this.fullClearsIn.length;
        for (var n = 0; n < t; ++n) {
            var r = this.fullClearsIn[n];
            if (r.delay > 0) {
                r.delay -= dt;
                if (r.delay <= 0) {
                    this.fullClearsIn.splice(n, 1);
                    n--;
                    t--;
                    this.clearCell(false, true);
                }
            } else {
                this.fullClearsIn.splice(n, 1);
                n--;
                t--;
            }
        }
        if (this.removeBlockIn > 0) {
            this.removeBlockIn -= dt;
            if (this.removeBlockIn <= 0 && (this.isBreakable || this.isBombBlocked || this.isWaterBlock || this.isPlantBlock))
                this.removeBlock();
        }
        this.timeSinceLastTypeChange += dt;
        if (this.object) {
            if (!(this.object.cell && this.object.cell != this && this.object.teleportTarget))
                this.object.cell = this;
            this.object.update(dt);
        }
        if (this.changeTypeIn > 0) {
            this.changeTypeIn -= dt;
            if (this.changeTypeIn <= 0 && this.object) {
                this.changeObjectType(this.typeToChange);
            }
        }
    }
}
var t = CellObject;
Object.defineProperty(t.prototype, "isChangingType", {
    get: function () {
        return this.changeTypeIn >= 0;
    },
    enumerable: true,
    configurable: true
});
Object.defineProperty(t.prototype, "isObjectBlocked", {
    get: function () {
        return this.colorBlock >= 0 || this.isWaterBlock || this.isPlantBlock && this.plantBreaksLeft == 2;
    },
    enumerable: true,
    configurable: true
});
t.prototype.prepareToChangeType = function (e, t) {
    if (typeof t === "undefined") {
        t = -1;
    }
    while (t < 0) {
        t =Utils.getInt(GameConst.level.assetNumber);
    }
    this.changeTypeIn = e;
    this.typeToChange = t;
};
t.prototype.setMark = function (e) {
    if (typeof e === "undefined") {
        e = 1;
    }
    if (e == this.marksLeft)
        return;
    this.prevMarkAlpha = 1;
    GameConst.level.needToUpdateBack = true;
    var t = this.marksLeft;
    this.marksLeft = e;
    GameConst.level.needToUpdateBack = true;
    if (t > 0 && e < t && this.mark && e >= 0)
        GameConst.level.objects.push(new CookieTweenSprite(this.mark.x, this.mark.y, e));
    if (this.marksLeft <= 0) {
        Utils.removeClip(this.mark);
        this.mark = null;
        GameConst.level.target.onMarkRemoved();
        return;
    }
    var n = e == 1 ? "cookie" : e == 2 ? "cookie honey" : "cookie x3";
    var mark = this.mark ? this.mark : Utils.createBitmap(n);
    mark.gotoAndStop(n);
    mark.x = GameConst.level.offsetX + this.x * GameConst.level.tileSize;
    mark.y = GameConst.level.offsetY + this.y * GameConst.level.tileSize;
    // mark.pivotX = mark.pivotY = 4;
    if (mark.parent == null)
        GameConst.level.markLayer.addChild(mark);
    this.mark = mark;
};
t.prototype.reset = function () {
    Utils.removeClip(this.block);
    Utils.removeClip(this.mark);
    Utils.removeClip(this.blockTop);
    this.blockTop = null;
    this.removeBlockLock = -1;
    this.isTeleportLocked = false;
    this.block = null;
    this.mark = null;
    this.isPushEnabled = true;
    this.lastBlockSet = t.lastBombRemove = -1;
    if (this.object) {
        GameConst.pool.returnObject(this.object);
        this.object = null;
    }
    this._isBlock = false;
    this.isTempBlock = false;
    this.tempBlockTime = -1;
    this.breakCountLeft = 0;
    this.marksLeft = 0;
    this.colorBlock = -1;
    this.breakMatchId = -1;
    this.clearsIn.length = 0;
    this.removeBlockIn = -1;
    this.timeSinceBlockRemove = 100;
    this.lastBlockSet = -1;
    this.scoreToAdd = -1;
    this.bonusComboCount = -1;
    this.teleportCellStart = null;
    this.dontRemoveMark = false;
    this.teleportCellEnd = null;
    if (this.blockAnimation) {
        this.blockAnimation.destroy();
        this.blockAnimation = null;
    }
    this.teleports.length = 0;
    this.lastTeleportMove = -1;
    Utils.removeClip(this.teleportSprite);
    this.teleportSprite = null;
    this.teleportCellEnd = this.teleportCellStart = null;
    this.changeTypeIn = -1;
    this.isBombBlocked = false;
    this.fullClearsIn.length = 0;
    this.timeSinceLastTypeChange = 0;
    this.isWaterBlock = false;
    t.waterRemoveMove = -1;
    this.isPlantBlock = false;
    this.plantBreaksLeft = -1;
};
t.prototype.onTeleport = function (e) {
    if (this.isTeleportLocked)
        return;
    if (GameConst.level.currentMove > this.lastTeleportMove) {
        this.teleports.length = 0;
        this.lastTeleportMove = GameConst.level.currentMove;
    }
    if (this.teleports.indexOf(e) != -1)
        this.isTeleportLocked = true;
    else
        this.teleports.push(e);
};
t.prototype.setTeleport = function (isIn, cell) {
    if (isIn)
        this.teleportCellEnd = cell;
    else
        this.teleportCellStart = cell;
   Utils.removeClip(this.teleportSprite);
    this.teleportSprite = Utils.createAnimation(isIn ? "teleport_in" : "teleport_out");
    GameConst.level.bonusLayer.addChild(this.teleportSprite);
    // this.teleportSprite.pivotX = this.teleportSprite.getBounds().width * .5;
    this.teleportSprite.pivotX = 0;
    this.teleportSprite.pivotY = isIn ? 10 : 0;
    this.teleportSprite.x = this.pos.x;
    this.teleportSprite.y = this.pos.y + GameConst.level.tileSize * (!isIn ? - .5 : .5)
};
t.prototype.prepareToTeleport = function (target) {
    this.teleportSprite.play(0,false);
    this.object.isMoving = true;
    target.setObject(this.object);
    this.teleportCellEnd.setObject(this.object);
    this.object.playAnimation(105, 1, 1);
    this.object.teleportTarget = target;
    this.onTeleport(this.object)
};
Object.defineProperty(t.prototype, "isMarked", {
    get: function () {
        return this.marksLeft > 0
    },
    enumerable: true,
    configurable: true
});
Object.defineProperty(t.prototype, "isColorBlocked", {
    get: function () {
        return this.colorBlock >= 0
    },
    enumerable: true,
    configurable: true
});
t.prototype.setBlock = function (type, n) {
    if (typeof n === "undefined") {
        n = -1
    }
    var r = type == 1 ? 1 : type == 2 ? 2 : type == 3 ? 3 : 0;
    var i = type == 4;
    var s = type == 5;
    var iswater = type == 6;
    var ismask = type == 7;
    if (this.isWaterBlock || this.isPlantBlock || this._isBlock)
        return;
    this.lastBlockSet = r;
    this.breakCountLeft = i ? 0 : r;
    this._isBlock = true;
    this.colorBlock = i ?Utils.getInt(GameConst.level.assetNumber) : -1;
    this.isBombBlocked = this.isBombBlocked || s;
    if ((i || iswater || ismask) && !GameConst.level.isGenerated) {
        this.setObject(GameConst.pool.getObject(this, GameConst.level.gridToStageX(this.x), GameConst.level.gridToStageY(this.y), GameConst.level.assetNumber, this.colorBlock));
        this.object.isMoving = false
    }
    if (iswater) {
        this.isWaterBlock = true;
        if (GameConst.level.isGenerated) {
            GameConst.level.objects.push(new SinglePlayObject(this.pos.x - 32, this.pos.y - 30, "water_splash",  GameConst.level.blockDestroyLayer, 10, 17, 1));
            SoundsManager.instance.playSound("water_set")
        }
        t.waterCells.push(this);
        this.setTempBlock(true, Math.max(.5, this.tempBlockTime));
        if (this.object && GameConst.level.isGenerated)
            this.object.playAnimation(117, 1, 1)
    }
    if (ismask) {
        this.isPlantBlock = true;
        this.plantBreaksLeft = 2;
        this.prevMarkAlpha = 1;
        GameConst.level.needToUpdateBack = true;
        var a = "bush root";
        var mark = this.mark ? this.mark : Utils.createBitmap(a);
        mark.gotoAndStop(a);
        mark.pivotX = mark.pivotY = 30;
        mark.x = GameConst.level.offsetX + (this.x + .5) * GameConst.level.tileSize;
        mark.y = GameConst.level.offsetY + (this.y + .5) * GameConst.level.tileSize;
        if (mark.parent == null)
            GameConst.level.markLayer.addChild(mark);
        this.mark = mark
    }
    var block = Utils.createSpriteFromSpritesheet( ismask ? "bush" : iswater ? "water" : s ? "mine_sprite_x" : i ? "wall color" : r == 0 ? "chocolate nuts" : r == 1 ? "chocolate white" : r == 2 ? "chocolate black" : "chocolate black x3 full");
    if (!s) {
        Utils.setReg(block, .5, .5);
        block.x = GameConst.level.offsetX + (this.x + .5) * GameConst.level.tileSize;
        block.y = GameConst.level.offsetY + (this.y + .5) * GameConst.level.tileSize
    } else {
        block.pivotX = 32;
        block.pivotY = 31;
        block.x = GameConst.level.offsetX + (this.x + .5) * GameConst.level.tileSize;
        block.y = GameConst.level.offsetY + (this.y + .5) * GameConst.level.tileSize
    }
    Utils.addChild(block, n >= 0 ? GameConst.level.blockDestroyLayer : GameConst.level.blockLayer);
    this.block = block;
    if (!i && !s && n >= 0 || iswater) {
        if (this.blockAnimation){
            this.blockAnimation.destroy();
        }
        if (!iswater){
            this.removeBlockLock = GameConst.level.currentMove;
        }
        this.blockAnimation = new AnimationObject(iswater ? GameConst.waterAnimation : GameConst.chocolateAnimation, block);
        this.blockAnimation.resetStopFrame = !iswater;
        this.blockAnimation.sprite.visible = iswater;
        if (!iswater) {
            var l = n / 30 + .001;
            this.fullClearsIn.push(new ClearData((n + 3) / 30 + .001, false));
            if (this.isMarked)
                this.dontRemoveMark = true;
            var c = t.blockAnimationFrames[n] - 1;
            this.blockAnimation.prepareToPlayAnimation(l, c);
            this.setTempBlock(true, Math.max(this.tempBlockTime, 1))
        } else {
            if (GameConst.level.isGenerated)
                this.blockAnimation.playAnimation(0, 1, 1);
            else
                this.blockAnimation.setFrame(11, true)
        }
        this.blockAnimation.handlePos = true
    }
};
t.prototype.changeObjectType = function (e) {
    if (e != this.getType()) {
        var t = this.object.isBonus;
        var n = this.object.bonusType;
        this.timeSinceLastTypeChange = 0;
        GameConst.pool.returnObject(this.object);
        this.setObject(GameConst.pool.getObject(this, GameConst.level.gridToStageX(this.x), GameConst.level.gridToStageY(this.y), 3, e));
        this.object.playJellyAnimation(1, 1);
        if (t)
            this.object.setBonusType(n)
    }
};
t.prototype.removeBlock = function () {
    if (!this.isObjectBlocked && (!this.isBreakable || this.breakMatchId == GameConst.level.currentMove) && !this.isBombBlocked && !this.isPlantBlock)
        return;
    if (this.removeBlockLock >= 0 && this.removeBlockLock >= GameConst.level.currentMove)
        return;
    if (this.isBombBlocked && t.lastBombRemove >= GameConst.level.currentMove)
        return;
    var e = this.block != null;
    if (this.clearsIn.length == 0 && this.fullClearsIn.length == 0)
        this.breakMatchId = GameConst.level.currentMove;
    var n = this.breakCountLeft;
    this.breakCountLeft--;
    this.timeSinceBlockRemove = 0;
    var r = this.isColorBlocked;
    if (this.isColorBlocked) {
        this.colorBlock = -1;
        this.setTempBlock(true, Math.max(this.tempBlockTime, .15));
       Utils.removeClip(this.blockTop);
        this.blockTop = null
    }
    if (this.mark) {
        Laya.Tween.clearAll(this.mark);
        if (this.mark.alpha < 1)
            Laya.Tween.to(this.mark, {
                alpha: 1
            }, 150, Laya.Ease.quadInOut)
    }
    var i = this.isWaterBlock;
    if (this.isWaterBlock) {
        t.waterRemoveMove = GameConst.level.currentMove;
        var s = t.waterCells.indexOf(this);
        while (s >= 0) {
            t.waterCells.splice(s, 1);
            s = t.waterCells.indexOf(this)
        }
        this.isWaterBlock = false;
        GameConst.level.waterRemoveMove = GameConst.level.currentMove;
        this.setTempBlock(true, Math.max(.5, this.tempBlockTime));
        if (this.object)
            this.object.playAnimation(117, 1, 1);
        SoundsManager.instance.playSound("water_remove")
    }
    var o = this.isBombBlocked;
    if (this.isBombBlocked) {
        t.lastBombRemove = GameConst.level.currentMove;
        this.isBombBlocked = false;
        var u = t.blockAnimationShifts.length;
        for (var s = 0; s < u; ++s) {
            var a = this.x + t.blockAnimationShifts[s].x;
            var f = this.y + t.blockAnimationShifts[s].y;
            if (a >= 0 && f >= 0 && a < GameConst.level.fieldWidth && f < GameConst.level.fieldHeight) {
                var l = GameConst.level.cells[a][f];
                if (l.isWaterBlock)
                    l.removeBlock();
                if (!l._isBlock) {
                    if (GameConst.level.target.isBlockTarget)
                        GameConst.level.target.blocksToRemove++;
                    l.setBlock(1, s)
                }
            }
        }
        this.setTempBlock(true, Math.max(this.tempBlockTime, 1.1))
    }
    var c = this.isPlantBlock;
    if (this.isPlantBlock) {
        this.plantBreaksLeft--;
        SoundsManager.instance.playSound(this.plantBreaksLeft == 0 ? "root_remove" : "bush_remove");
        this.isPlantBlock = this.plantBreaksLeft > 0;
        this._isBlock = this.plantBreaksLeft == 2;
        if (this.plantBreaksLeft <= 0)
            GameConst.level.target.onBushRemoved()
    }
    if (GameConst.level.isActive && !c && !i)
        SoundsManager.instance.playSound(o ? "chocolate_bomb" : r ? "color_crash" : "choco_crash");
    if (this.breakCountLeft <= 0 && !c) {
        if (r)
            GameConst.level.objects.push(new SinglePlayObject(this.block.x, this.block.y, "color_wall",  GameConst.level.blockDestroyLayer, 53, 53, GameConst.ANIM_SCALE));
        else if (i)
            GameConst.level.objects.push(new SinglePlayObject(this.pos.x - 32, this.pos.y - 30, "water_splash",  GameConst.level.blockDestroyLayer, 10, 17, 1));
        else if (o)
            GameConst.level.objects.push(new SinglePlayObject(GameConst.level.offsetX + (this.x + .5) * GameConst.level.tileSize, GameConst.level.offsetY + (this.y + .5) * GameConst.level.tileSize, "mine", GameConst.level.blockDestroyLayer, 43, 62, 1));
        else if (this.lastBlockSet >= 2)
            GameConst.level.objects.push(new SinglePlayObject(this.block.x, this.block.y, "chocolate",  GameConst.level.blockDestroyLayer, 53, 65, GameConst.ANIM_SCALE));
        else
            GameConst.level.objects.push(new SinglePlayObject(this.block.x, this.block.y, "chocolatewhite",  GameConst.level.blockDestroyLayer, 57, 58, GameConst.ANIM_SCALE));
        this._isBlock = false;
        if (!i || !this.blockAnimation) {
           Utils.removeClip(this.block);
            if (this.blockAnimation)
                this.blockAnimation.destroy();
            this.blockAnimation = null
        } else {
            this.blockAnimation.destroyOnReset = true;
            this.blockAnimation.playAnimation(13, 1, 1)
        }
        this.block = null
    } else if (c) {
        if (this.plantBreaksLeft == 1) {
            this.block.gotoAndPlay("bush_anim");
            this.block.pivotX = this.block.pivotY = 35;
            this.block.framerate = 30;
            this.setTempBlock(true, Math.max(this.tempBlockTime, 20 / this.block.framerate))
        } else if (this.plantBreaksLeft <= 0) {
            this.plantBreaksLeft = -1;
            this.isPlantBlock = false;
            GameConst.level.objects.push(new CookieTweenSprite(this.mark.x, this.mark.y, 5));
           Utils.removeClip(this.mark);
            this._isBlock = false;
            this.mark = null;
            GameConst.level.needToUpdateBack = true;
           Utils.removeClip(this.block);
            this.block = null
        }
    } else if (this.breakCountLeft == 2) {
        this.block.gotoAndStop("chocolate black");
        GameConst.level.objects.push(new CookieTweenSprite(this.pos.x, this.pos.y, 10))
    } else if (this.breakCountLeft == 1) {
        this.block.gotoAndStop("chocolate black2");
        this.block.pivotX += 2;
        this.block.pivotY += 2
    }
    if (e && this.block == null && (n > 0 || o) && !i && !r && !c)
        GameConst.level.target.onBlockRemoved()
};
t.prototype.setTempBlock = function (isTempBlock, time) {
    if (typeof time === "undefined") {
        time = -1
    }
    this.isTempBlock = isTempBlock;
    this.tempBlockTime = time
};
t.prototype.isBlock = function () {
    return this._isBlock || this.isTempBlock
};
Object.defineProperty(t.prototype, "isBreakable", {
    get: function () {
        return this._isBlock && this.breakCountLeft > 0
    },
    enumerable: true,
    configurable: true
});
t.prototype.setObject = function (e) {
    this.object = e;
    if (this.object)
        this.object.cell = this
};
t.prototype.getSprite = function () {
    return this.object ? this.object.sprite : null
};
t.prototype.getType = function () {
    return this.object == null ? -1 : this.object.colorType
};
t.prototype.getBonusType = function () {
    return this.object == null ? 0 : this.object.bonusType
};
t.prototype.clearCell = function (e, t) {
    if (typeof e === "undefined") {
        e = true
    }
    if (typeof t === "undefined") {
        t = false
    }
    var n = false;
    if (this.object && (!this.object.isPushable || !GameConst.level.isActive) && !this.isObbjectBlocked) {
        if (GameConst.level.isActive && !t) {
            if (!(this.object.bonusType >= 1 && this.object.bonusType <= 4))
                GameConst.pool.getDestroyAnimation(this.object.colorType, this.object.sprite.x, this.object.sprite.y, this);
            if (this.object.bonusType == 4)
                this.tryPlayScoreAnimation();
            if (this.object.isBonus) {
                var r = this.object.bonusType;
                if (r == 1 || r == 2 || r == 5 || r == 6) {
                    SoundsManager.instance.playSound("bonus_line");
                    new BonusLineSprite(r, this.object.sprite.x, this.object.sprite.y)
                } else if (r == 3 || r == 7) {
                    new BonusBombSprite(r, this.object.sprite.x, this.object.sprite.y);
                    SoundsManager.instance.playSound("bonus_bomb")
                } else if (r == 4)
                    SoundsManager.instance.playSound("bonus_color");
                LevelManager.instance.bonuses++
            }
        }
        GameConst.level.target.onObjectRemove(this.object);
        GameConst.pool.returnObject(this.object);
        this.object = null;
        n = true
    }
    if (this.marksLeft > 0 && n && !this.dontRemoveMark) {
        this.setMark(this.marksLeft - 1);
        SoundsManager.instance.playSound("cookie_crash")
    }
    if (this.dontRemoveMark) {
        this.dontRemoveMark = false
    }
    var i = this.isBreakable;
    if (this.isBreakable || this.isObjectBlocked || this.isBombBlocked || this.isPlantBlock && n) {
        this.removeBlock()
    }
    if (e && !i) {
        for (var s = 0; s < 4; ++s) {
            var o = this.x + (s == 0 ? 1 : s == 1 ? -1 : 0);
            var u = this.y + (s == 2 ? 1 : s == 3 ? -1 : 0);
            if (o >= 0 && u >= 0 && o < GameConst.level.fieldWidth && u < GameConst.level.fieldHeight) {
                var a = GameConst.level.cells[o][u];
                if (a && (a.isBreakable || a.isBombBlocked || a.isWaterBlock) && (!a.isColorBlocked && !a.isPlantBlock || a.colorBlock == this.getType())) {
                    a.removeBlockIn = .01;
                    var f = Math.max(this.tempBlockTime, a.tempBlockTime);
                    if (f <= 0)
                        f = GameConst.GEM_KILL_DELAY * 1.1;
                    if (f >= 0)
                        a.setTempBlock(true, Math.max(a.tempBlockTime, f))
                }
            }
        }
    }
};
Object.defineProperty(t.prototype, "blockWasRemovedRecently", {
    get: function () {
        return this.timeSinceBlockRemove < .5
    },
    enumerable: true,
    configurable: true
});
t.prototype.onSpellExplosion = function (e) {
    if (typeof e === "undefined") {
        e = -1
    }
    if (this.object && !this.object.isMoving) {
        this.object.onSpellExplosion(e)
    }
};
t.prototype.isStable = function () {
    return this.object == null || !this.object.isMoving
};
t.prototype.isStatic = function () {
    return !this.object.isMoving
};
t.prototype.tryPlayScoreAnimation = function () {
    if (this.scoreToAdd > 0) {
        var e = GameConst.level.isEnded;
        if (e)
            this.scoreToAdd = 10 * Math.round(this.scoreToAdd * 1.25 / 10);
        GameConst.level.objects.push(GameConst.pool.getText(this.scoreToAdd.toString(), this, 0));
        this.scoreToAdd = -1;
        this.bonusComboCount = -1
    }
};
Object.defineProperty(t.prototype, "minClearIn", {
    get: function () {
        var e = this.clearsIn.length;
        if (e == 0)
            return -1;
        var t = 1e10;
        for (var n = 0; n < e; ++n)
            t = Math.min(t, this.clearsIn[n].delay);
        return t
    },
    enumerable: true,
    configurable: true
});
/**
 * 清理前的动画处理
 */
t.prototype.prepareToClear = function (e, t, n, r) {
    if (typeof e === "undefined") {
        e = -1
    }
    if (typeof t === "undefined") {
        t = 0
    }
    if (typeof n === "undefined") {
        n = -1
    }
    if (typeof r === "undefined") {
        r = false
    }
    var i = t == 0;
    var s = t == 4;
    var o = .01 + e;
    this.clearsIn.push(new ClearData(o, i || s));
    if (this.object && (this.object.isBonus || r)) {
        var u = this.object.bonusType <= 3 || r ? GameConst.BonusDestroyAnimationTIME : this.object.bonusType == 4 ? 20 / 30 : -1;
        if (u > 0) {
            var a = this.object.playBonusAnimationIn;
            this.object.playBonusAnimationIn = Math.min(a < 0 ? 1e10 : a, o > u ? o - u - .01 : -1)
        }
    }
};
Object.defineProperty(t.prototype, "isWaitingForClear", {
    get: function () {
        return this.clearsIn.length > 0
    },
    enumerable: true,
    configurable: true
});


t.lastBombRemove = -1;
t.waterRemoveMove = -1;
t.waterCells = [];
t.BASE_SCORE = 10;
t.BONUS_CELL_SCORE_FACTOR = 5;
t.BONUS_SCORE_FACTOR = 2;
t.BONUS_COLOR_SCORE_FACTOR = 4;
t.CACHE_NONE = 0;
t.CACHE_CLEAR = 1;
t.CACHE_DRAW = 2;
t.blockAnimationFrames = [2, 37, 72, 107, 142, 177, 212, 248];
t.blockAnimationShifts = [Utils.cjp(0, -1), Utils.cjp(1, -1), Utils.cjp(1, 0), Utils.cjp(1, 1), Utils.cjp(0, 1), Utils.cjp(-1, 1), Utils.cjp(-1, 0), Utils.cjp(-1, -1)];


